﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WXY.Course.Common;
using WXY.Course.Model;

namespace WXY.Course.Web.Areas.Admin.Controllers
{
    public class TransferController : Controller
    {
        private static Doc2Swf _doc2Swf = new Doc2Swf();
        private const string TEMP_FLASH_PATH = "swf";

        [HttpPost]
        public JsonResult ToPdf(string file)
        {
            file = HttpUtility.UrlDecode(file);
            string filePath = file;
            if (!file.StartsWith("http", StringComparison.CurrentCultureIgnoreCase))
            {
                filePath = HttpContext.Server.MapPath("~/" + file.TrimStart('/'));
            }
            try
            {
                string path = "/" + TEMP_FLASH_PATH + "/" + DateTime.Now.ToString("yyMMddHHmmssffff") + ".pdf";

                string toPath = HttpContext.Server.MapPath("~" + path);
                bool result = _doc2Swf.TransferDocToPdf(filePath, toPath);
                return Json(new BoolResult<string> { IsSuccess = result, Data = path, Message = result ? "成功" : "失败" });
            }
            catch (Exception ex)
            {
                return Json(new BoolResult<string> { IsSuccess = false, Data = string.Empty, Message = ex.Message });
            }
        }
    }
}
